


$(document).ready(function(){
	
	findRoleList();
});


function findRoleList(){
	  $.ajax({
        url: urlcore + "/api/adminRole/findRoleList",
        type: "get",
        dataType: "json",
        data:{},
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        success: function (data) {
        $("#thislist").html("");
        var da=data.data;
        var str="",html="";
	        for(var i=0;i<da.length;i++){
	        	if(da[i].available==1){
	        		str="可用";
	        	}else if(da[i].available==2){
	        		str="不可用";
	        	}
	       html += "  <tr>\n" +
	            "<td>" + (i + 1) + "</td>\n" +
	            "<td>" + da[i].name+ "</td>\n" +
	            " <td>" + str + "</td>\n" ;
	            if(da[i].available==1){
	    html += "  \t\t<td>\n"+
	            " <a class=\"btn btn-danger btn-xs\" href=\"javascript:;\" onclick=\"agreeWithDrawals(" + da[i].id + ",3)\"> 禁用</a>\n" +
	            " <a class=\"btn btn-danger btn-xs\" href=\"javascript:;\" onclick=\"agreeWithDrawals(" + da[i].id + ",3)\"> 删除</a>\n" +
	            " <a class=\"btn btn-success btn-xs\" href=\"javascript:;\" data-toggle=\"modal\" data-target=\"#add\"  onclick=\"addPermission("+da[i].id+")\"> 添加权限</a>\n" +
	            " </td>\n" +
	    		" </tr>";
	            }else if(da[i].available==2){
	   html +=  "  \t\t<td>\n"+
	            " <a class=\"btn btn-success btn-xs\" href=\"javascript:;\" onclick=\"agreeWithDrawals(" + da[i].id + ",3)\"> 启用</a>\n" +
	            " </td>\n" +
	    		" </tr>";
	            }
	        }
	        $("#thislist").append(html);
        },
        error: function (data) {
            alert(data.msg);
        }
    })
	
}



//获取所有权限
function addPermission(id) {
	sessionStorage.setItem("roleId",id);
	$('#menu').html("");
	$.ajax({
			url: urlcore + "/api/adminPermission/selectList?roleId="+id,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success:function(data){
				var da = data.data;
				var checkbox = "";
				$.each(da,function(i,n){
					if(n.checked==true){
						checkbox +='<label><input type="checkbox" name="catalog" value="'+n.id+'"  checked>'+n.name+'</label>';
					}else{
						checkbox +='<label><input type="checkbox" name="catalog" value="'+n.id+'"  >'+n.name+'</label>';
					}
					
				});
				$('#menu').append(checkbox);
			}
		});
$('#this_title').html('添加权限');

}



//添加/修改
function toAdd() {
	var rodeId=sessionStorage.getItem("roleId");
	var catalogId="";
	var len = $('input[name="catalog"]:checked').length;
	$('input[name="catalog"]:checked').each(function(i,n){
		if(i == len-1){
   	 	catalogId += $(this).val();
   	 }else{
   	 	catalogId += $(this).val()+",";
   	 }
	});    

	  $.ajax({
        url: urlcore + "/api/adminRolePermission/add?rodeId="+rodeId+"&catalogId="+catalogId,
        type: "get",
        dataType: "json",
        data:{},
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        success: function (data) {
        	if (data.success == true) {
        		location.reload();
        	}else{
        			alert(data.msg)
        	}
        },
        error: function (data) {
            alert(data.msg);
        }
    })
		
	
}







//加载修改数据
function updateAdmin(id,userName,nick,realName) {
	thistitle();//获取权限列表
    $('#this_title').html('修改管理员');
	$('#password_add').css("display", "none");
	$('#password_add_label').css("display", "none");
	$('#aginPassword_add').css("display", "none");
	$('#aginPassword_add_label').css("display", "none");	
	$('#adminId').val(id);
	$('#userName_add').val(userName);
	$('#nick_add').val(nick);
	$('#realName_add').val(realName);	
	$.ajax({
		url: urlcore + "/api/adminAuthority/getCatalogByAdminId?id=" + id,
		type: "POST",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			var da = data.data;	
			$.each(da,function(i,n){
				$("input:checkbox[value='"+n.catalogId+"']").attr("checked","checked");
			});
			
		},error:function() {
			alert("error");
		}
	})

}




